package server

import (
	"context"
	"longmen/server/pkg/db/mysql/db_sports"
	pb "longmen/server/pkg/protobuf"
)

func (s Server) FootBallInitialOdds(ctx context.Context, request *pb.MatchRequest) (*pb.FootballInitOddsResp, error) {
	initialOdds, err := db_sports.GetFootballInitialOdds(int(request.MatchId), request.ExType)
	if err != nil {
		return nil, err
	}
	items := make([]*pb.OddsItem, 0)
	for _, odd := range initialOdds {
		items = append(items, odd.GetProto())
	}

	realTimeOdds, err := db_sports.GetFootballRealTimeOdds(int(request.MatchId), request.ExType)
	if err != nil {
		return nil, err
	}
	realTime := make([]*pb.OddsItem, 0)
	for _, realTimeOdd := range realTimeOdds {
		realTime = append(realTime, realTimeOdd.GetProto())
	}
	return &pb.FootballInitOddsResp{
		InitItems:     items,
		RealTimeItems: realTime,
	}, nil
}
func (s Server) FootBallHistoryOdds(ctx context.Context, req *pb.HistoryOddsRequest) (*pb.FootballOddsResp, error) {
	historyOdds, err := db_sports.GetFootballHistoryOdds(int(req.Match.MatchId), int(req.CompanyId), req.Match.ExType)
	if err != nil {
		return nil, err
	}
	items := make([]*pb.OddsItem, 0)
	for _, odd := range historyOdds {
		items = append(items, &pb.OddsItem{
			Id:               odd.Id,
			MatchId:          uint32(odd.MatchId),
			CompanyId:        int32(odd.CompanyId),
			ExponentialType:  odd.ExponentialType,
			ExponentialScore: odd.ExponentialScore,
			MatchStatus:      uint32(odd.MatchStatus),
			OddsStatus:       uint32(odd.OddsStatus),
			Odds1:            odd.Odds1.String(),
			Odds2:            odd.Odds2.String(),
			Odds3:            odd.Odds3.String(),
			ChangeTime:       odd.ChangeTime,
			CompetitionTime:  odd.CompetitionTime,
		})
	}
	return &pb.FootballOddsResp{
		Items: items,
	}, nil
}

func (s Server) BasketBallInitialOdds(ctx context.Context, req *pb.MatchRequest) (*pb.BasketBallInitOddsResp, error) {
	historyOdds, err := db_sports.GetBasketBallInitialOdds(int(req.MatchId), req.ExType)
	if err != nil {
		return nil, err
	}
	items := make([]*pb.OddsItem, 0)
	for _, odd := range historyOdds {
		items = append(items, odd.GetProto())
	}

	realTime, err := db_sports.GetBasketBallRealTimeOdds(int(req.MatchId), req.ExType)
	if err != nil {
		return nil, err
	}
	realTimeOdds := make([]*pb.OddsItem, 0)
	for _, realOdd := range realTime {
		realTimeOdds = append(realTimeOdds, realOdd.GetProto())
	}
	return &pb.BasketBallInitOddsResp{
		InitItems:     items,
		RealTimeItems: realTimeOdds,
	}, nil
}
func (s Server) BasketBallHistoryOdds(ctx context.Context, req *pb.HistoryOddsRequest) (*pb.BasketBallOddsResp, error) {
	historyOdds, err := db_sports.GetBasketBallHistoryOdds(int(req.Match.MatchId), int(req.CompanyId), req.Match.ExType)
	if err != nil {
		return nil, err
	}
	items := make([]*pb.OddsItem, 0)
	for _, odd := range historyOdds {
		items = append(items, odd.GetProto())
	}
	return &pb.BasketBallOddsResp{
		Items: items,
	}, nil
}
